/**
 * 
 */
package com.gr.staffpm.charts;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.wicketstuff.flot.Color;
import org.wicketstuff.flot.DataSet;
import org.wicketstuff.flot.GraphType;


/**
 * @author Graham Rhodes 6 Apr 2011 09:59:55
 */
public class Series implements Serializable {

    /** Required by {@link Serializable} */
    private static final long serialVersionUID = 1L;

    String label;
    Color color;
    Set<GraphType> graphTypes;
    List<DataSet> data;

    public Series(List<DataSet> data, String label, Color color, Set<GraphType> graphTypes) {
        this.label = label;
        this.color = color;
        this.graphTypes = new HashSet<GraphType>(graphTypes);
        this.data = new ArrayList<DataSet>(data);
    }

    public Series(List<DataSet> data, String label, Color color, GraphType... graphTypes) {
        this(data, label, color, new HashSet<GraphType>(Arrays.asList(graphTypes)));
    }

    public String getLabel() {
        return label;
    }

    public List<DataSet> getData() {
        return Collections.unmodifiableList(data);
    }

    @Override
    public String toString() {
        StringBuffer str = new StringBuffer();
        str.append("{data: [");
        for (DataSet dataSet : getData()) {
            str.append(dataSet.toString());
            str.append(", ");
        }
        // Remove last ", "
        if (getData().size() > 0)
            str.setLength(str.length() - 2);
        str.append("], label: \"" + getLabel() + "\", color: \"" + color.html() + "\"");

        if (graphTypes.size() > 0)
            str.append(", ");

        for (GraphType graphType : graphTypes) {
            str.append(graphType);
            str.append(", ");
        }

        if (graphTypes.size() > 0) {
            str.setLength(str.length() - 2);
            str.append("}");
        }

        return str.toString();
    }

    public Series addDataSet(DataSet dataSet, Color color) {
        List<DataSet> newData = new ArrayList<DataSet>(data);
        newData.add(dataSet);
        return new Series(newData, label, color, graphTypes);
    }
}
